package cn.edu.jxau.dao;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.edu.jxau.domain.reply;
import cn.edu.jxau.domain.user;
import cn.edu.jxau.domain.userInfo;
import cn.itcast.jdbc.TxQueryRunner;

public class userInfoDao {

	private QueryRunner qr = new TxQueryRunner();

	/**
	 * 查询用户信息
	 */
	public HashMap<user, userInfo> queryUserInfo() {
		HashMap<user, userInfo> map = new HashMap<user, userInfo>();
		String sql = "select *from user";
		try {
			List<user> users = qr.query(sql, new BeanListHandler<user>(user.class));
			sql = "select *from userInfo where username=?";
			for (user user : users) {
				userInfo userInfo = qr.query(sql, new BeanHandler<userInfo>(userInfo.class), user.getUsername());
				map.put(user, userInfo);
			}
			return map;
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}

	/**
	 * 通过username改变用户状态
	 */

	public void changeStatusByUsername(String username, String state) {
		String sql = "update userinfo set state=? where username=?";
		Object[] param = { state, username };
		try {
			qr.update(sql, param);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}

	/**
	 * 根据用户名查询发的帖子数 若没查询到则返回0
	 * 
	 * @param username
	 * @return
	 */
	public int findTotal(String username) {
		String sql = "select count(*) from post where username=?";
		try {
			// Number total= (Number)qr.query(sql, new ScalarHandler(),
			// username);
			if (qr.query(sql, new ScalarHandler(), username) != null) {
				return ((Number) qr.query(sql, new ScalarHandler(), username)).intValue();
			} else {
				return 0;
			}
		} catch (SQLException e) {

		}
		return 0;
	}

	/**
	 * 根据用户名查询用户邮箱
	 * 
	 * @param username
	 * @return
	 */
	public String findMailInfoByUsername(String username) {
		String sql = "select * from user where username=?";
		try {
			return (String) qr.query(sql, new ScalarHandler(), username);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}

	/**
	 * 根据用户名查询用户表userInfo表的信息
	 * 
	 * @param username
	 * @return
	 */
	public userInfo findUserInfoByUsername(String username) {
		String sql = "select * from userInfo where username=?";
		try {
			return qr.query(sql, new BeanHandler<userInfo>(userInfo.class), username);
		} catch (SQLException e) {
			throw new RuntimeException();
		}
	}

	public void updateImg(String realname, String username) {
		// TODO Auto-generated method stub
		String sql="update userinfo set headPortrait=? where username=?";
		String[] params={realname,username};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 这是根据指定用户名查询出该用户回复了哪些主题
	 * @param username
	 * @return
	 */
	public List<Map<String, Object>> findMyReply(String username) {
		// TODO Auto-generated method stub
		String sql="select title,reply_cintent from reply,post where reply_postid=postId and reply_user=?";
		try {
			return qr.query(sql, new MapListHandler(), username);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
		
	}
	/**
	 * 这是根据用户名查询我的主题哪些人回复了
	 */
	public List<Map<String,Object>> findReplyMy(String username){
		String sql="select reply_content,reply_user,title from post,reply where poster=? and postId=reply_postid";
		try {
			return qr.query(sql, new MapListHandler(), username);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
		}
	}
}
